// map.js
import qqMap from '../../../utils/qqmap'
Page({
  data: {
    polyline:[],
    detail:{},
    //默认坐标
    latitude: 28.216106,
    longitude: 112.862015,
    markers: [
      {
        id: 1,
        latitude: 28.216106,
        longitude: 112.862015,
        width: 24,
        height: 30,
      },
      {
        id: 2,
        latitude: 28.18393,
        longitude: 112.935489,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40,
      },
    ],
  },
  onShow(){
    this.getDetail()
    this.getPolyline()
  },
  async getDetail(){
    const res = await wx.http({
      url:'/repair/'+this.options.id
    })
    console.log(res);
    this.setData({detail:res.data})
  },
  //取消报修，并更新
  async cancelRepair() {
    await wx.http({
      method: 'put',
      url: '/cancel/repaire/' + this.options.id,
    })
    // 更新列表数据中的状态
    const pages = getCurrentPages()
    const listPage = pages.find((v) => v.route === 'repair_pkg/pages/list/index')
    listPage.updateRepairItemStatus(this.options.id, 0)

    wx.navigateBack()
  },
  //编辑
  goEdit() {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index?id=' + this.options.id,
    })
  },

  // 调用位置服务（路线规划）
  getPolyline() {
    console.log(111);
    qqMap.direction({
      mode: 'bicycling',
      from: '28.18393,112.935489',
      to: '28.216106,112.862015',
      success: ({ res }) => {
        const ret =res
        const coors = ret.result.routes[0].polyline,
        points = []
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / 1000000
        }
        // 获取经纬度
        for (let i = 0; i < coors.length; i += 2) {
          points.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        // 渲染数据
        this.setData({
          latitude: points[0].latitude,
          longitude: points[0].longitude,
          polyline: [
            {points, color: '#5591af', width: 4},
          ],
        })
      },
    })
  },
})
